package com.lzs.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzs.PO.Area;
import com.lzs.PO.Family;
import com.lzs.PO.Member;
import com.lzs.PO.Page;
import com.lzs.Service.FamilyService;
import com.lzs.Service.MemberService;
import com.lzs.Service.PartdetailService;
import com.lzs.util.Msg;

@Controller
@RequestMapping(value = "/family")
public class FamilyController {

	@Autowired
	FamilyService familyService;
	@Autowired
	MemberService memberService;
	@Autowired
	PartdetailService partdetailService;

	@RequestMapping(value = "/family_list")
	public String familyList(Model model, HttpServletRequest request, @RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "5") Integer pageSize, String keyword) throws Exception {

		Page<Family> page = this.familyService.findFamilyPagelist(keyword, pageSize, pageNum);
		
//		PageHelper.startPage(pageNum, pageSize);
//		
//		List<Family> list = this.familyService.findFamilyList(null);
//		
//		 PageInfo<Family> pageInfo = new PageInfo(list); 
		 
		System.out.println(page.getList());
		
//		System.out.println("pageinfo对象"+pageInfo.getList());
		model.addAttribute("page", page);
		
//		model.addAttribute("pageinfolist", pageInfo.getList());

		return "/family/family_list";

	}

	@RequestMapping(value = "/family_add")
	public String addFamily() {

		return "/family/family_add";
	}

	@RequestMapping(value = "/family_check")
	@ResponseBody
	public Msg checkFamily(String family_adress) {

		Boolean b = this.familyService.countFamilyByAdress(family_adress);

		if (b) {

			return Msg.success();

		} else {

			return Msg.fail();

		}
	}

	@ResponseBody
	@RequestMapping(value = "/family_add_action")
	public Msg addFamilyAction(String member_name, String people_id, String medical_card_id, String rural_id,
			String member_id, String relation, String family_adress, String family_id, String area_county_id,
			String area_town_id, String area_village_id, String area_team_id) {
          
		 try {
			 Map<String,Object> map = new HashMap<String,Object>();
			 
			 map.put("member_name", member_name);
			 
			 map.put("people_id", people_id);
			 
			 map.put("medical_card_id", medical_card_id);
			 
			 map.put("rural_id", rural_id);
			 
			 map.put("member_id", member_id);
			 
			 map.put("relation", relation);
			 
			 map.put("family_adress", family_adress);
			 
			 map.put("family_id", family_id);
			 
			 map.put("area_county_id", area_county_id);
			 
			 map.put("area_town_id", area_town_id);
			 
			 map.put("area_village_id", area_village_id);
			 
			 map.put("area_team_id", area_team_id);
			 
			 this.familyService.addFamily(map);
			 
			 this.memberService.addMember(map);
			 
			 return Msg.success();
			 
		 }catch(Exception e) {
			 
			 e.printStackTrace();
			 
			 return Msg.fail();
			 
		 }
	}
	@RequestMapping(value = "/family_update")
	public String updateFamily(String family_id,Model model) {
		
		Family family = this.familyService.findFamilyByFamilyId(family_id);
		
		Member member = this.memberService.findMemberByFamilyId(family_id);
		
		model.addAttribute("family", family);
		
		model.addAttribute("member", member);
		
		return "/family/family_update";
	}
	@ResponseBody
	@RequestMapping(value = "/family_update_action")
	public Msg updateFamilyAction(String member_name, String people_id, String medical_card_id, String rural_id,
			String member_id, String relation, String family_adress, String family_id, String area_county_id,
			String area_town_id, String area_village_id, String area_team_id) {
		
		try {
			
            Map<String,Object> map = new HashMap<String,Object>();
			 
			 map.put("member_name", member_name);
			 
			 map.put("people_id", people_id);
			 
			 map.put("medical_card_id", medical_card_id);
			 
			 map.put("rural_id", rural_id);
			 
			 map.put("member_id", member_id);
			 
			 map.put("relation", relation);
			 
			 map.put("family_adress", family_adress);
			 
			 map.put("family_id", family_id);
			 
			 map.put("area_county_id", area_county_id);
			 
			 map.put("area_town_id", area_town_id);
			 
			 map.put("area_village_id", area_village_id);
			 
			 map.put("area_team_id", area_team_id);
			 
			 this.familyService.updateFamily(map);
			 
			 this.memberService.updateMember(map);
			
			return Msg.success();
			
		}catch(Exception e) {
			
			e.printStackTrace();
			
			return Msg.fail();
			
		}
}
	@ResponseBody
	@RequestMapping(value = "/family_delete")
	public Msg deleteFamily(String family_id) {
		//1.通过family_id查出所有家庭成员的rural_id
		List<Member> memberList = this.memberService.findMembersByFamilyId(family_id);
		//2.用每个家庭成员的rural_id去参合详情表中查询记录，将记录添加到List中
		List<Member> memberlistWithPart = new ArrayList<Member>();
		for(int i=0;i<memberList.size();i++) {
			
			if (this.partdetailService.findPartdetailByRuralId(memberList.get(i).getRural_id()) != null) {
				memberlistWithPart.add(this.partdetailService.findPartdetailByRuralId(memberList.get(i).getRural_id()));
			}
			System.out.println("删除请求的家庭业务:"+memberlistWithPart);
		}
		//3.判断List是否为null，如果为null则删除家庭及家庭成员记录，如果不为null则不能删除
		if(memberlistWithPart.isEmpty()) {
			
			this.familyService.deleteFamily(family_id);
			
			this.memberService.deleteMember(family_id);
			
			return Msg.success();
			
		}else {
			
			return Msg.fail();
			
		}
	}
}
